//
//  HHButtonTableListCell.m
//  developKit
//
//  Created by Hayder on 2022/12/19.
//

#import "HHButtonTableListCell.h"

@interface HHButtonTableListCell()

@property (nonatomic, strong) UIControl *eventControl;

@end

@implementation HHButtonTableListCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        [self.blankView addSubview:self.titleLabel];
        [self.blankView addSubview:self.textField];
        [self.blankView addSubview:self.eventControl];
    }
    return self;
}

- (void)clickButtonEvent
{
    if(self.item.didClickBttonCellBlock)
    {
        self.item.didClickBttonCellBlock(self.item);
    }
}

- (void)setItem:(HHButtonListItem *)item
{
    _item = item;
    
    [self setConfigWithItem:item];
    
    self.titleLabel.text = item.title;
    self.textField.placeholder = item.placeholder;
    self.textField.text = item.contentText?:@"";
    
    [self setNeedsLayout];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat contentW = self.blankView.width;
    CGFloat contentH = self.blankView.height;
    
    self.titleLabel.frame = CGRectMake(self.margin, 0, self.item.titleDefaultWidth, contentH);
    self.textField.frame = CGRectMake(self.titleLabel.right + self.margin, (contentH - 30)/2, contentW - self.titleLabel.right - 2*self.margin, 30);
    self.eventControl.frame = CGRectMake(self.titleLabel.right + self.margin, 0, contentW - self.titleLabel.right - 2*self.margin, contentH);
}

- (UILabel *)titleLabel
{
    if(!_titleLabel)
    {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = Font(15);
        _titleLabel.textColor = kGray_51;
    }
    return _titleLabel;
}

- (UITextField *)textField
{
    if(!_textField)
    {
        _textField = [[UITextField alloc] init];
        _textField.font = Font(15);
        _textField.textColor = kGray_51;
        _textField.enabled = NO;
    }
    
    return _textField;
}

- (UIControl *)eventControl
{
    if(!_eventControl)
    {
        _eventControl = [[UIControl alloc] init];
        [_eventControl addTarget:self action:@selector(clickButtonEvent) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _eventControl;
}

@end
